<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :model="queryParams" ref="queryForm" label-width="80px" inline>
      <el-form-item label="疾病名称">
        <el-input v-model="queryParams.name" placeholder="请输入疾病名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <div style="margin-bottom: 10px;">
      <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
      <el-button type="danger" icon="el-icon-delete" :disabled="!multipleSelection.length" @click="handleDelete">删除</el-button>
    </div>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="疾病名称" />
      <el-table-column prop="description" label="描述" /> <!-- 修改：字段改为 description -->
      <el-table-column label="操作" width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-show="total > 0"
      background
      layout="total, prev, pager, next, sizes"
      :total="total"
      :page-size="queryParams.pageSize"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
      @size-change="handleSizeChange"
    />

    <!-- 新增/编辑弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="form" ref="form" label-width="80px">
        <el-form-item label="疾病名称" prop="name" :rules="[{ required: true, message: '请输入疾病名称', trigger: 'blur' }]">
          <el-input v-model="form.name" placeholder="请输入疾病名称" />
        </el-form-item>
        <el-form-item label="疾病描述" prop="description"> <!-- 修改：字段改为 description -->
          <el-input v-model="form.description" placeholder="请输入疾病描述" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDisease, getDisease, addDisease, updateDisease, delDisease } from '@/api/hospital/disease'


export default {
  name: "Disease",
  data() {
    return {
      loading: false,
      tableData: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: ""
      },
      form: {},
      dialogVisible: false,
      dialogTitle: "",
      multipleSelection: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listDisease(this.queryParams).then(res => {
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = { pageNum: 1, pageSize: 10, name: "" };
      this.getList();
    },
    handleSizeChange(size) {
      this.queryParams.pageSize = size;
      this.getList();
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
    },
    handleAdd() {
      this.form = {};
      this.dialogTitle = "新增疾病";
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.form = { ...row };
      this.dialogTitle = "修改疾病";
      this.dialogVisible = true;
    },
    handleDelete(row) {
      const ids = row ? [row.id] : this.multipleSelection.map(item => item.id);
      this.$confirm("是否确认删除选中记录?", "提示").then(() => {
        delDisease(ids).then(() => {
          this.$message.success("删除成功");
          this.getList();
        });
      });
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const api = this.form.id ? updateDisease : addDisease;
          api(this.form).then(() => {
            this.$message.success("操作成功");
            this.dialogVisible = false;
            this.getList();
          });
        }
      });
    }
  }
};
</script>
